//
//  YTTabBarController.m
//  CllForIOS
//
//  Created by 杨涛涛 on 2018/3/5.
//  Copyright © 2018年 aRui. All rights reserved.
//

#import "YTTabBarController.h"
#import "YTHomeViewController.h"
#import "YTDiscoverViewController.h"
#import "YTMineViewController.h"

#import "YTHallVC.h"

#import "YTNavigationController.h"
@interface YTTabBarController ()

@end

@implementation YTTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSMutableDictionary *data = [Tookit getUserCasheInfo];
    if ([[data objectForKey:@"whiteList2"] isEqual:@(YES)]) {
        for (UIView *view in self.tabBar.subviews) {
            [view removeFromSuperview];
        }
        [self createViewControllersfour];
        [self setTabbarfour];
    }else{
        for (UIView *view in self.tabBar.subviews) {
            [view removeFromSuperview];
        }
        [self createViewControllers];
        [self setTabbar];
    }

}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //获取白名单
    NSMutableDictionary *data = [Tookit getUserCasheInfo];
               if ([[data objectForKey:@"whiteList2"] isEqual:@(YES)]) {
                for (UIView *view in self.tabBar.subviews) {
                    [view removeFromSuperview];
                }
                [self createViewControllersfour];
                [self setTabbarfour];
            }else{
                for (UIView *view in self.tabBar.subviews) {
                    [view removeFromSuperview];
                }
//                [self createViewControllers];
//                [self setTabbar];
                [self createViewControllersfour];
                [self setTabbarfour];
            }
    self.selectedIndex = 0;
}

- (void)createViewControllersfour{
    YTHomeViewController * one = [[YTHomeViewController alloc]init];
    UINavigationController * oneNav = [[YTNavigationController alloc]initWithRootViewController:one];
    YTDiscoverViewController * two = [[YTDiscoverViewController alloc]init];
    UINavigationController * twoNav = [[UINavigationController alloc]initWithRootViewController:two];
    YTHallVC* three = [[YTHallVC alloc]init];
    YTNavigationController * threeNav = [[YTNavigationController alloc]initWithRootViewController:three];
    YTMineViewController * four = [[YTMineViewController alloc]init];
    YTNavigationController * fourNav = [[YTNavigationController alloc]initWithRootViewController:four];
    
    self.viewControllers = @[oneNav,twoNav,threeNav,fourNav];
}
- (void)createViewControllers{
    YTHomeViewController * one = [[YTHomeViewController alloc]init];
    UINavigationController * oneNav = [[YTNavigationController alloc]initWithRootViewController:one];
    YTDiscoverViewController * two = [[YTDiscoverViewController alloc]init];
    UINavigationController * twoNav = [[UINavigationController alloc]initWithRootViewController:two];
    YTMineViewController * three = [[YTMineViewController alloc]init];
    YTNavigationController * threeNav = [[YTNavigationController alloc]initWithRootViewController:three];
    
    self.viewControllers = @[oneNav,twoNav,threeNav];
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    self.selectedIndex = item.tag;
}

- (void)setTabbarfour{
    NSArray * titleArray = @[@"赛事赛程",@"发现",@"奇云大厅",@"个人中心"];
    NSArray * imageNormalArray = @[@"home_unsele",@"dis_unsele",@"mine_unsele",@"exper_unsele"];
    NSArray * imageSelArray =@[@"home_sele",@"dis_sele",@"mine_sele",@"exper_sele"];
    for (int i = 0; i < titleArray.count; i ++) {
        UITabBarItem * tabbarItem =[[UITabBarItem alloc]init];
        tabbarItem.title = titleArray[i];
        UIImage * normalImage = [IMAGE(imageNormalArray[i]) imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIImage * selImage = [IMAGE(imageSelArray[i]) imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        tabbarItem.image = normalImage;
        tabbarItem.selectedImage = selImage;
        //        [tabbarItem setTitlePositionAdjustment:UIOffsetMake(0, -4)];//-4
        
        UIViewController *VC = self.viewControllers[i];
        [tabbarItem setTag:i];
        //向下偏移6距离，不然有偏差
        //        tabbarItem.imageInsets =UIEdgeInsetsMake(6,0, -6,0);
        VC.tabBarItem = tabbarItem;
        
    }
    self.tabBar.backgroundImage = [UIImage scaleToSize:[UIImage imageNamed:@"tabbarbg"] size:CGSizeMake(self.tabBar.frame.size.width,55)];
    self.tabBar.shadowImage = [[UIImage alloc] init];
    
    [self.tabBar setTintColor:[UIColor whiteColor]];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: RGBA(94, 94, 94), NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:RGBA(94, 94, 94), NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
    for (UIView *view in self.tabBar.subviews) {
        
        if ([view isKindOfClass:[UIImageView class]] && view.bounds.size.height <= 1) {
            UIImageView *ima = (UIImageView *)view;
            //            ima.backgroundColor = [UIColor redColor];
            ima.hidden = YES;
        }
    }
}
- (void)setTabbar{

    NSArray * titleArray = @[@"赛事赛程",@"发现",@"个人中心"];
    NSArray * imageNormalArray = @[@"home_unsele",@"dis_unsele",@"mine_unsele"];
    NSArray * imageSelArray =@[@"home_sele",@"dis_sele",@"mine_sele"];
    for (int i = 0; i < titleArray.count; i ++) {
        UITabBarItem * tabbarItem =[[UITabBarItem alloc]init];
        tabbarItem.title = titleArray[i];
        UIImage * normalImage = [IMAGE(imageNormalArray[i]) imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIImage * selImage = [IMAGE(imageSelArray[i]) imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        tabbarItem.image = normalImage;
        tabbarItem.selectedImage = selImage;
//        [tabbarItem setTitlePositionAdjustment:UIOffsetMake(0, -4)];//-4
        
        UIViewController *VC = self.viewControllers[i];
        [tabbarItem setTag:i];
        //向下偏移6距离，不然有偏差
//        tabbarItem.imageInsets =UIEdgeInsetsMake(6,0, -6,0);
        VC.tabBarItem = tabbarItem;
        
    }
    self.tabBar.backgroundImage = [UIImage scaleToSize:[UIImage imageNamed:@"tabbarbg"] size:CGSizeMake(self.tabBar.frame.size.width,55)];
    self.tabBar.shadowImage = [[UIImage alloc] init];
    
    [self.tabBar setTintColor:[UIColor whiteColor]];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: RGBA(94, 94, 94), NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:RGBA(94, 94, 94), NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
    for (UIView *view in self.tabBar.subviews) {
        
        if ([view isKindOfClass:[UIImageView class]] && view.bounds.size.height <= 1) {
            UIImageView *ima = (UIImageView *)view;
            //            ima.backgroundColor = [UIColor redColor];
            ima.hidden = YES;
        }
    }
}

@end
